package com.cn.thread;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
/**
 * @author hels
 * @version 1.0
 * @date 2022/3/8 10:41
 */
public class ThreadPoolDemo1 {
    public static void main(String[] args) {
        // https://www.jianshu.com/p/f030aa5d7a28
        // LinkedBlockingQueue（无界阻塞队列）如果任务提交速度大于任务处理速度，会造成队列大量阻塞。OOM，FixedThreadPool任务执行无序
        ExecutorService service = Executors.newFixedThreadPool(2);

        Runnable r = new MyRunnable();
        Runnable r2 = new Runnable() {
            @Override
            public void run() {
                System.out.println("来了一个练习者" + Thread.currentThread().getName());
            }
        };
        // 从线程池中获取对象，然后调用MyRunnable中的run方法
        service.submit(r);
        service.submit(r);
        service.submit(r2);
        // 关闭线程池
        service.shutdown();
    }
}


class MyRunnable implements Runnable{
    @Override
    public void run() {
        System.out.println("我要一个教练" + Thread.currentThread().getName());
        try {
            Thread.sleep(2000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("他来了，教完课又回去了");
    }
}
